//
//  ViewController.swift
//  手账
//
//  Created by zhuxiaoxiang on 2018/11/15.
//  Copyright © 2018年 zhuxiaoxiang. All rights reserved.
//

import UIKit

enum MyError: Error {
    case error1NameIsNil
    case error2NameLengthIs0
}

class ViewController: BaseViewController {
    private lazy var adLayout: ADLayout = {
        let one = ADLayout()
        one.delegate = self
        return one
    }()
    
    fileprivate lazy var adCollectionView: ADCollectionView = {
       let one = ADCollectionView(frame: CGRect(x: 0, y: 100, width: kScreenW, height: 100), collectionViewLayout: self.adLayout)
        one.adDelegate = self
        one.set()
        return one
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.showMessage(AppUser.sharedInstance.phone ?? "还未登录")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.present(LoginViewController(), animated: true, completion: nil)
    }
}

extension ViewController {
    func setUpUI() {
        self.view.addSubview(self.adCollectionView)
    }
}

extension ViewController: ADLayoutProtocol, ADCollectionViewProtocol {
    var itemSize: CGSize {
        get {
            return CGSize(width: kScreenW - 100, height: 100)
        }
    }
    
    var dataArray: [NSObject] {
        get {
            return [UIImage(named: "ad1.jpg")!, UIImage(named: "ad2.jpg")!, UIImage(named: "ad3.jpg")!, UIImage(named: "ad4.jpg")!]
        }
    }
    
    var interval: TimeInterval {
        get {
            return 3
        }
    }
    
    var reuseIdentifier: String {
        get {
            return "CCELL"
        }
    }
}

extension ViewController {
    func testThrows(_ name: String?) throws {
        guard name != nil else {
            throw MyError.error1NameIsNil
        }
        guard name!.characters.count > 0 else {
            throw MyError.error2NameLengthIs0
        }
    }
}


